#!/usr/bin/env python3

import sys, random, json
from common import fget

def main (argv0, argv):
    settings = json.loads(fget("settings.json"))
    headers = {
        "Content-Type": settings["content_type"]
    }
    body = fget("main.html.fmt").format(r=random.random(), **settings)
    
    headers["Content-Length"] = len(body)
    
    response  = "".join("{}: {}\r\n".format(k, v) for k, v in headers.items())
    response += "\r\n"
    response += body
    sys.stdout.buffer.write(response.encode("UTF-8"))

if __name__ == "__main__":
    try:
        sys.exit(main(sys.argv[0], sys.argv[1:]))
    except KeyboardInterrupt:
        pass
